package uml2Java;



import java.io.File;
import java.util.Map;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage.Registry;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.UMLResource;

public class UMLChargeur  {
	
	protected String _path;

	public UMLChargeur(String path) {
		_path = path;
	}

	public Resource loadResource() {
		ResourceSet resourceSet = new ResourceSetImpl();
		URI fileURI =  URI.createFileURI(new File(_path).getAbsolutePath());
		Registry packageRegistry = resourceSet.getPackageRegistry();

		packageRegistry.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);

		Resource poResource = resourceSet.getResource(fileURI, true);
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
		Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap();
		URI uri = URI.createURI("jar:file:/home/bastien/Programmes/eclipse/plugins/org.eclipse.uml2.uml.resources_2.2.0.v200805131030.jar!/");
		uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), uri.appendSegment("metamodels").appendSegment(""));
		//uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), uri.appendSegment("libraries").appendSegment(""));
		//uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), uri.appendSegment("profiles").appendSegment(""));

		return poResource;
	}
	
	public Package loadUMLPackage() {
		return (Package)loadResource().getContents().get(0);
	}

	public EList<Package> loadUMLSubPackages() {
		return loadUMLPackage().getNestedPackages();
	}

	protected void setSpecificLoader(Registry packageRegistry) {
	}

}
